home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’95 / TrashHack / TrashHackInstaller.c < prev    next >
Text File  |  1995-06-24  |  2KB  |  111 lines

  1.  
  2. // DrawStringPatch.a
  3.  
  4. // patches DrawString to draw the size of the trash instead of "Trash"
  5.  
  6. #include    <GestaltEqu.h>
  7. #include    <Memory.h>
  8. #include    <OSUtils.h>
  9. #include    <Resources.h>
  10. #include    <Traps.h>
  11. #include    <Types.h>
  12.  
  13. // turn debugs off
  14. #define        DebugStr(x)
  15.  
  16. #define        kDrawStringTrap    0xa884
  17. #define        kJumpAbsoluteInstr    0x4ef9
  18.  
  19. #define        rPatchResourceType    'DRWS'
  20. #define        rPatchResourceID    0
  21.  
  22. pascal void
  23. MAIN (void)
  24. {
  25.     Boolean            foundJump;
  26.     long            i;
  27.     long            codeSize;
  28.     long            drawStringAddress;
  29.     unsigned short    *code;
  30.     Handle            drawStringPatchHandle;
  31.     
  32.     // GET CURRENT ADDRESS OF DRAWSTRING TRAP
  33.     
  34.     // figure out where DrawString goes now
  35.     drawStringAddress = NGetTrapAddress (kDrawStringTrap, ToolTrap);
  36.     
  37.     // do we have it?
  38.     if (drawStringAddress == NGetTrapAddress (_Unimplemented, ToolTrap))
  39.     {
  40.         DebugStr ("\p DrawString not implemented on this machine");
  41.         return;
  42.     }
  43.  
  44.     // LOAD DRAWSTRING PATCH CODE RESOURCE
  45.     
  46.     // make sure our resource goes into the system heap
  47.     SetZone (SystemZone ());
  48.     
  49.     // get our patch code resource
  50.     drawStringPatchHandle = Get1Resource (rPatchResourceType, rPatchResourceID);
  51.     
  52.     // did we get it?
  53.     if (drawStringPatchHandle == nil)
  54.     {
  55.         DebugStr ("\p Can't find patch code resource");
  56.         return;
  57.     }
  58.     
  59.     // lock it down
  60.     HNoPurge (drawStringPatchHandle);
  61.     HLock (drawStringPatchHandle);
  62.     
  63.     // PATCH JUMP INSTRUCTION
  64.     
  65.     // take an instruction pointer into the code
  66.     code = (unsigned short *) *drawStringPatchHandle;
  67.     
  68.     // say we haven't found the jump instruction
  69.     foundJump = false;
  70.     
  71.     // figure out how many WORDS are in the code resource
  72.     codeSize = GetHandleSize (drawStringPatchHandle) << 1;
  73.     
  74.     // for each word in the code resource
  75.     for (i = 0; i < codeSize; i++)
  76.     {
  77.         // is it our jump instruction?
  78.         if (code [i] == kJumpAbsoluteInstr)
  79.         {
  80.             // patch in the current NewGWorld trap address
  81.             code [i + 1] = drawStringAddress >> 16;
  82.             code [i + 2] = drawStringAddress & 0xffff;
  83.             
  84.             // we're done
  85.             foundJump = true;
  86.             
  87.             break;
  88.         }
  89.     }
  90.     
  91.     // SET DRAWSTRING TRAP ADDRESS
  92.     
  93.     // check we found the jump instruction
  94.     if (foundJump)
  95.     {
  96.         // install our code in the system heap
  97.         DetachResource (drawStringPatchHandle);
  98.         
  99.         // set the trap address
  100.         NSetTrapAddress ((UniversalProcPtr) *drawStringPatchHandle, kDrawStringTrap, ToolTrap);
  101.     }
  102.     else
  103.     {
  104.         DebugStr ("\p Couldn't find jump instruction");
  105.         
  106.         // dump the resource
  107.         ReleaseResource (drawStringPatchHandle);
  108.     }
  109. }
  110.  
  111.